<template>
  <el-dialog @close="close" v-model="dialogVisible" :title="title" width="50%">
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="120px">
      <el-form-item label="寿命时长" prop="onlinetime">
        <el-input type="number" v-model="ruleForm.onlinetime" placeholder="请输入寿命时长" >
          <template #append>时（H）</template>
        </el-input>
      </el-form-item>

      <el-form-item label="每日强制时长" prop="mandatory_time">
        <el-input type="number" v-model="ruleForm.mandatory_time" placeholder="请输入每日强制时长" >
          <template #append>时（H）</template>
        </el-input>
      </el-form-item>

      <el-form-item label="每月监测日" prop="mandatory_time">
        <el-input type="number" v-model="ruleForm.remainingday" placeholder="请输入每月监测日" >
          <template #append>日</template>
        </el-input>
      </el-form-item>

    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="saveLoading" @click="handleConfim(ruleFormRef)">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ElMessageBox, ElMessage, FormInstance, ElNotification } from 'element-plus'
import { reactive, ref } from 'vue'
import { editEquiSetting} from '@/api/admin'
import { useRoute } from 'vue-router'
const route = useRoute()
const ruleFormRef = ref<FormInstance>()
const dialogVisible = ref<boolean>(false)
const title = ref('设备配置')
const checkIsPositive = (rule, value, callback) => {
  if (!value) {
    callback()
  } else {
    const reg = /^\d+(?=\.{0,1}\d+$|$)/
    if (reg.test(value)) {
      callback()
    } else {
      callback(new Error('请输入大于等于0的正数'))
    }
  }
}
const rules = reactive({
  // type_id: [{ required: true, message: '请选择设备平台', trigger: 'change' }],
  // name: [{ required: true, message: '请输入设备名称', trigger: 'blur' }],
  // pix: [{ required: true, message: '请输入前缀标识', trigger: 'blur' }],
  onlinetime: [{ validator: checkIsPositive, trigger: 'blur' }],
  mandatory_time: [{ validator: checkIsPositive, trigger: 'blur' }],
  remainingday: [{ validator: checkIsPositive, trigger: 'blur' }],
  // platform_id: [{ required: true, message: '请选择医院', trigger: 'change' }],
  // room_id: [{ required: true, message: '请选择医院', trigger: 'change' }],
})
const ruleForm = reactive({
  equipment_id: '',
  room_id: '',
  mandatory_time: '',
  platform_id: '',
  onlinetime: '',
  remainingday:''
})
const saveLoading = ref(false)
const emit = defineEmits(['refresh'])
const platData = ref()
const roomData = ref()
const props = defineProps({
  typeArr: {
    type: Array,
    default: [],
  },
})

function close() {
  ruleFormRef.value.resetFields()
  Object.keys(ruleForm).forEach((key) => {
    ruleForm[key] = null
  })
}

const show = (item = {}) => {
  ruleForm.equipment_id = item['id'];
  ruleForm.room_id = item['room_id'];
  ruleForm.platform_id = item['platform_id'];
  ruleForm.onlinetime = item['setting']?item['setting'].onlinetime:'';
  ruleForm.mandatory_time = item['setting']?item['setting'].mandatory_time:'';
  ruleForm.remainingday = item['setting']?item['setting'].remainingday:'';
  dialogVisible.value = true
}

const handleConfim = async (done: () => void) => {
  await ruleFormRef.value.validate((valid, fields) => {
    if (valid) {
      if (ruleForm.remainingday>28 || ruleForm.remainingday<1) {
        ElMessage.warning('检测日必须在1-28')
        return
      }
      saveLoading.value = true
        //编辑
        editEquiSetting(ruleForm).then((res) => {
          if (res.code == 200) {
            ElNotification({
              title: '编辑成功',
              message: res.msg,
              type: 'success',
            })
            emit('refresh')
          }
          dialogVisible.value = false
          saveLoading.value = false
        })
    }
  })
}


defineExpose({
  show,
})
</script>
<style lang="scss" scoped></style>
